package com.woniu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Road;
import com.woniu.service.IMaintainerServices;
import com.woniu.service.IPatrolServices;
import com.woniu.service.IRoadService;
import com.woniu.vo.RoadQueryReturnVo;
import com.woniu.vo.RoadQueryVo;
import com.woniu.vo.RoadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqg
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/road")
@Api(tags = "路段管理")
@Slf4j
public class RoadController extends BaseController{
    @Autowired
    private IRoadService roadService;
    @Autowired
    private IPatrolServices patrolServices;
    @Autowired
    private IMaintainerServices maintainerServices;

    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("分页条件查询")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody RoadQueryVo roadQueryVo) {
        IPage<RoadQueryReturnVo> search = roadService.search(currentPage, pageSize, roadQueryVo);
        PageResult pageResult = new PageResult();
        if (search != null) {
            pageResult.setTotal(search.getTotal());
            pageResult.setData(search.getRecords());
        } else {
            pageResult.setTotal(0L);
            pageResult.setData(null);
        }
        return toDataResult(pageResult);
    }

    @PostMapping
    @ApiOperation("新增路段")
    public Result add(@RequestBody RoadVo roadVo) {
        boolean save = roadService.add(roadVo);
        log.debug("操作的状态码为",save);
        return toResult(save);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除路段")
    public Result remove(@PathVariable Long id) {
        return toResult(roadService.removeById(id));
    }

    @PutMapping
    @ApiOperation("根据id修改路段")
    public Result edit(@RequestBody RoadVo roadVo) {

        boolean result = roadService.edit(roadVo);

        return toResult(result);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result getById(@PathVariable Long id) {
        RoadQueryReturnVo returnVo = roadService.searchById(id);
        return toDataResult(returnVo);
    }


    @GetMapping
    @ApiOperation("获取全部")
    public Result getAll() {
        return toDataResult(roadService.list());
    }

    @GetMapping("/getAllPatrol")
    @ApiOperation("获取全部巡检员")
    public Result getAllPatrol() {
        return toDataResult(patrolServices.list());
    }

    @GetMapping("/getAllMaintainer")
    @ApiOperation("获取全部运维员")
    public Result getAllMaintainer() {
        return toDataResult(maintainerServices.list());
    }
}
